from sqlalchemy import TEXT, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .base import TimedBaseModel
from .m2m import calendars_check_messages


class Calendar(TimedBaseModel):
    __tablename__ = "calendar"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    title_ru: Mapped[str] = mapped_column(String(255))
    title_en: Mapped[str] = mapped_column(String(255))
    title_uk: Mapped[str] = mapped_column(String(255))
    description_ru: Mapped[str] = mapped_column(TEXT, nullable=True)
    description_en: Mapped[str] = mapped_column(TEXT, nullable=True)
    description_uk: Mapped[str] = mapped_column(TEXT, nullable=True)
    final_message_ru: Mapped[str] = mapped_column(TEXT)
    final_message_en: Mapped[str] = mapped_column(TEXT)
    final_message_uk: Mapped[str] = mapped_column(TEXT)
    category = relationship("Category", back_populates="calendars")
    category_id: Mapped[int] = mapped_column(ForeignKey("category.id"))
    tasks = relationship("CalendarTask", back_populates="calendar")
    check_messages = relationship(
        "CheckMessage", secondary=calendars_check_messages, back_populates="calendars"
    )
    users = relationship("UserCalendar", back_populates="calendar")

    def __repr__(self):
        return f"<Календарь №{self.id} {self.title_ru}>"

    def __str__(self):
        return self.__repr__()
